package com.foreveross.proxyip;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;

/**
 * 代理ip
 * @author Administrator
 *
 */
public class ProxyIpModel {
	
	public final static int ENBLE = 1;
	public final static int UNENBLE = 0;
	public final static int ALL_UNENBLE = 2;
	public final static int REPEAT_ENBLE = 3; //重复可用状态，完成任务后还是可用的
	
	public final static int IP_LOCATION_RMI = 0;
	public final static int IP_LOCATION_SPOOL = 1;
	public final static int IP_LOCATION_CPOOL = 2;
	
	/**
	 * 使用者标识
	 */
	private Serializable userId;
	/**
	 * 使用后状态，1可用，0不可用
	 */
	private Integer useStatus = ENBLE;
	/**
	 * 是否已回收
	 */
	private Boolean hasRecycle = false;
	/**
	 * 创建时间
	 */
	private Date createTime = new Date();
	/**
	 * 记录渠道使用状态
	 */
	private Map<String, Boolean> channelStatusMap = null;
	
	private String ip;
	
	private int port;
	/**
	 * 使用次数
	 */
	private int useCount = 0;
	
	private Object proxyObject;
	/**
	 * 代理IP所属的位置，0远程统一接口代理IP , 1本地简单池代理IP,　2本地渠道池代理IP
	 */
	private int location = IP_LOCATION_RMI;
	public ProxyIpModel(){}
	public ProxyIpModel(String ip, int port) {
		super();
		this.ip = ip;
		this.port = port;
		channelStatusMap = new HashMap<String, Boolean>();
	}

	public void countUseCount(){
		this.useCount += 1; 
	}
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public Object getProxyObject() {
		return proxyObject;
	}

	public void setProxyObject(Object proxyObject) {
		this.proxyObject = proxyObject;
	}

	public Serializable getUserId() {
		return userId;
	}

	public void setUserId(Serializable userId) {
		this.userId = userId;
	}

	public Integer getUseStatus() {
		return useStatus;
	}

	public void setUseStatus(Integer useStatus) {
		this.useStatus = useStatus;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public int getUseCount() {
		return useCount;
	}
	public void setUseCount(int useCount) {
		this.useCount = useCount;
	}
	
	public int getLocation() {
		return location;
	}
	public void setLocation(int location) {
		this.location = location;
	}
	/**
	 * 当前IP渠道可用状态记录
	 * @param channelId
	 */
	public void addAbleStatus(String channelId){
		this.channelStatusMap.put(channelId, true);
	}
	/**
	 * 当前IP渠道不可用状态记录
	 * @param channelId
	 */
	public void addEnableStatus(String channelId){
		this.channelStatusMap.put(channelId, false);
	}
	/**
	 * 获得代理IP渠道状态key
	 * @return
	 */
	public List<String> getChannelStatusMapKeys() {
		return Lists.newArrayList(channelStatusMap.keySet());
	}
	/**
	 * 目前channelStatusMap只放不可用状态的，所以采用包含判断当前IP不可访问某个渠道
	 * @param channelId
	 * @return
	 */
	public boolean isExcludeChannel(String channelId){
		return this.channelStatusMap.containsKey(channelId);
	}
	
	public Boolean getHasRecycle() {
		return hasRecycle;
	}
	public void setHasRecycle(Boolean hasRecycle) {
		this.hasRecycle = hasRecycle;
	}
	public String getProxyIp(){
		return String.format("%s:%s", this.getIp(), this.getPort());
	}
	
}
